<template>
  <div class="login">
    <div class="middle-wrapper">
      <div class="title-wrapper">
        <div class="title">环策环保管理系统</div>
      </div>
      <div class="login-form">
        <el-form
          ref="loginForm"
          :model="form"
          :rules="rules">
          <el-form-item prop="username" label="账号" >
            <el-input v-model="form.username" placeholder="请输入账号">
              <i slot="prefix" class="el-input__icon el-icon-adm-user el-icon-user" style="font-size: 18px;"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input  type="password" v-model="form.password" placeholder="请输入密码"  @keyup.enter.native="loginHandle">
              <i slot="prefix" class="el-input__icon el-icon-adm-password el-icon-lock" style="font-size: 18px;"></i>
            </el-input>
          </el-form-item>
          <el-form-item style="margin-bottom: 0;">
            <el-col :span="10" :offset="14">
              <el-form-item>
                <el-button
                  type="primary"
                  class="login-btn"
                  style="width: 100%;"
                  @click="loginHandle">
                  登录系统
                </el-button>
              </el-form-item>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
      
    </div>

    <div class="foolder">
       ©2021 四川环策安全环保技术服务有限公司 版权所有
      <br/>
      <span class="safeUrl" @click="safeUrl()">蜀ICP备2021019368号</span>
    </div>
    
  </div>
</template>
<script>
  import validateCode from '@/components/ValidateCode/index';

  export default {
    
    components: {
      validateCode
    },
    data () {
      var checkYanzhengma = (rule, value, callback) => {
        value = value.toUpperCase();
        if (value === '') {
          callback(new Error('请输入验证码'));
        } else if (value !== this.checkCode) {
          callback(new Error('验证码错误'));
          this.$refs['validate-code'].draw();
        } else {
          callback();
        }
      };
      return {
        checkCode: '',
        form: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ]
        }
      };
    },
    created () {

    },
    mounted() {
      
    },
    methods: {
      safeUrl() {
        window.open("https://beian.miit.gov.cn/","_blank");  
      },
      loginHandle () {
        this.$refs['loginForm'].validate(valid => {
          if (valid) {
            this.$WLoading();
            this.$postJson('/login', this.form)
            .then(reponse => {
              if(reponse.code == 200) {
                const token = reponse.data.tokenHead + reponse.data.token;
                window.localStorage.setItem('token', token);
                this.getUserInfo();
              }else {
                this.$message.error(reponse.msg);
              }
              this.$cancelLoading();
            }).catch(err =>{
              this.$cancelLoading();
              this.$message.error('登录异常！');
            })
          }
        });      
      },
      getUserInfo () {
        let that = this;
        this.$get('/simple/user/info')
        .then(reponse => {
          if(reponse.code == 200) {
            that.$store.commit('set_user', reponse.data);
            window.localStorage.setItem('user', JSON.stringify(reponse.data));
          }else {
            this.$message.error(reponse.msg);
          }
          //页面跳转
          let path = this.$route.query.redirect;
          this.$router.replace((path == '/' || path == undefined) ? '/home' : path);
        }).catch(err =>{
          this.$message.error('获取用户信息异常！');
        })
      }
     
    }
  };
</script>
<style lang="scss" scoped>
.login {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #e4f2fa;
    background-image: url('../assets/login_bak.png');

    .middle-wrapper {
        position: fixed;
        width: 100%;
        margin: 0 auto;
        top: 50%;
        transform: translateY(-60%);

        .title-wrapper {
        margin-top: 50px;
        margin-bottom: 30px;
        text-align: center;
        font-size: 30px;
        font-weight: bold;
        color: #409EFF;
        }
        .login-form {
        position: relative;
        margin: 0 auto;
        width: 520px;
        padding: 30px 50px;
        box-sizing: border-box;
        border-radius: 5px;
        background-color: #ffffff;

        .el-row {
            margin-bottom: 20px;

            &:last-child {
            margin-bottom: 0;
            }

            .login-btn {
            width: 100%;
            }
        }

        .check-code-wrapper {
            display: flex;
            justify-content: space-between;
            height: 40px;

            .yanzhengma-wrapper {
            flex: 0 1 auto;
            }

            .validate-code-wrapper {
            flex: 0 0 160px;
            }
        }
        }
    }
}
.el-button--primary {
    color: #FFF;
    background-color: #409EFF;
    border-color: #409EFF;
}
.el-button--primary:focus, .el-button--primary:hover {
    background-color: #409EFF;
    border-color: #409EFF;
color: #FFF;
}
.foolder{
    position: fixed;
    width: 100%;
    margin: 0 auto;
    top: 96%;
    transform: translateY(-60%);

    .safeUrl:hover{
      color: #409EFF;
      cursor: pointer;
    }
}
</style>



